#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "feedbackwidget.h"
#include "suggestion.h"
#include "mapwidget.h"
#include "analysiswidget.h"
#include "exhibitionwidget.h"
#include "reservationwidget.h"
#include "adminwidget.h"
#include "session.h"
#include "logindialog.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setBackground();

    // 动态添加，注意使用小写类名
    ui->stackedWidget->addWidget(new mapwidget(this));         // index 0
    ui->stackedWidget->addWidget(new analysiswidget(this));    // index 1
    ui->stackedWidget->addWidget(new exhibitionwidget(this));  // index 2
    ui->stackedWidget->addWidget(new reservationwidget(this)); // index 3
    ui->stackedWidget->addWidget(new feedbackwidget(this));    // index 4
    ui->stackedWidget->addWidget(new adminwidget(this));
    ui->stackedWidget->addWidget(new suggestionwidget(this));
    ui->stackedWidget->setCurrentIndex(0); // 默认显示地图模块
    ui->stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    ui->stackedWidget->setMinimumSize(800, 600);
    if (!Session::isAdmin()) {
            ui->pushButtonht->hide();  // 隐藏按钮
        } else {
            ui->pushButtonht->show();  // 显示按钮（一般默认是显示，可以不写）
        }
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::setBackground()
{
    QPixmap pixmap("C:/Users/m1937/Pictures/Screenshots/bck.png");
    pixmap = pixmap.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); // 拉伸填满
    QPalette palette;
    palette.setBrush(QPalette::Window, QBrush(pixmap));
    this->setPalette(palette);
    this->setAutoFillBackground(true);
}
void MainWindow::on_btnMap_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::on_btnAnalysis_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_btnExhibition_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_btnReservation_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
}

void MainWindow::on_btnFeedback_clicked()
{
    ui->stackedWidget->setCurrentIndex(4);
}


void MainWindow::on_pushButtonht_clicked()
{
    if (!Session::isAdmin()) {
            QMessageBox::warning(this, "权限不足", "只有管理员可以访问管理后台！");
            return;
        }
        ui->stackedWidget->setCurrentIndex(5);
}

void MainWindow::on_pushButtonLogout_clicked()
{
    Session::setUsername("");
        Session::setVisitorId(-1);
        Session::setIsAdmin(false);  // 如果你有这个接口的话

        this->close(); // 关闭主窗口

        // 弹出登录窗口（模式对话框）
        LoginDialog loginDialog;
        if (loginDialog.exec() == QDialog::Accepted) {
            // 登录成功，重新创建 MainWindow 并显示
            MainWindow *mainWin = new MainWindow();
            mainWin->show();
        } else {
            // 用户关闭登录窗口，程序可以退出或者做其他处理
            qApp->quit();
        }
}

void MainWindow::on_pushButtonsuggest_clicked()
{
    ui->stackedWidget->setCurrentIndex(6);
}
